import {handleActions} from 'redux-actions';

import { COMPANYS_QUERY, ENTERPRISE_CREATE_CLEAR, COMPANYS_DELETE, COMPANYS_DELETE_NOTIFY, COMPANY_NOTIFY, COMPANY_NOTIFY_CLEAR, ENTERPRISE_CREATE } from '../actions/ActionTypes.js'

const enterprise = handleActions({
  [COMPANYS_QUERY.REQUEST](state, action) {
    const pagination = {...state.pagination, pageSize:action.payload.pageSize, current:action.payload.current };
    return {...state, pagination, loading: true,};
  },
  [COMPANYS_QUERY.SUCCESS](state, action) {
    const pagination = { ...state.pagination, total:action.payload.total };
    return {...state, data: action.payload.data, pagination, loading: false,};
  },
  [COMPANYS_QUERY.FAILURE](state, action) {
    return {...state, err: action.err, loading: false,};
  },
  [COMPANYS_DELETE.REQUEST](state) {
    return {...state, loading: true,};
  },
  [COMPANYS_DELETE.SUCCESS](state, action) {
    return {...state, loading: false,};
  },
  [COMPANYS_DELETE.FAILURE](state, action) {
    return {...state, err: action.err, loading: false,};
  },
  [COMPANYS_DELETE_NOTIFY](state, action) {
    const data = state.data.filter((company) => {
      return action.payload.ids.findIndex((id, index, ids) => {return (id == company.id)}) < 0;
    });
    return {...state, data, loading:false };
  },
  [COMPANY_NOTIFY.REQUEST](state) {
    return {...state, loading: true,};
  },
  [COMPANY_NOTIFY.SUCCESS](state) {
    return {...state, loading: false, success: true};
  },
  [COMPANY_NOTIFY.FAILURE](state, action) {
    return {...state, err: action.err, loading: false,};
  },
  [COMPANY_NOTIFY_CLEAR](state) {
    return {...state, success:false};
  },
  [ENTERPRISE_CREATE_CLEAR](state) {
    return {...state, success:false};
  },
  [ENTERPRISE_CREATE.REQUEST](state) {
    return {...state, loading: true,};
  },
  [ENTERPRISE_CREATE.SUCCESS](state) {
    return {...state, loading: false, success: true};
  },
  [ENTERPRISE_CREATE.FAILURE](state, action) {
    return {...state, err: action.err, loading: false,};
  },


}, {
  loading:false,
  data:[],
  pagination:{
    pageSize:10,
    current:1,
    total:0,
  },
  err:{},
  success:false,
});

export default enterprise;
